package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("AddressDAO")
@Transactional
public class AddressDAOImpl extends AbstractJpaDao<Address> implements AddressDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Address.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public AddressDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Address findAddressById(Integer id) throws DataAccessException {

		return findAddressById(id, -1, -1);
	}

	@Transactional
	public Address findAddressById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAddressById", startResult, maxRows, id);
			return (Address) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Address> findAddressByName(String name) throws DataAccessException {

		return findAddressByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAddressByName", startResult, maxRows, name);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByNameContaining(String name) throws DataAccessException {

		return findAddressByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByLongitude(BigDecimal longitude) throws DataAccessException {

		return findAddressByLongitude(longitude, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByLongitude(BigDecimal longitude, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByLongitude", startResult, maxRows, longitude);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByLatitude(BigDecimal latitude) throws DataAccessException {

		return findAddressByLatitude(latitude, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByLatitude(BigDecimal latitude, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByLatitude", startResult, maxRows, latitude);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByRegionalcode(String regionalcode) throws DataAccessException {

		return findAddressByRegionalcode(regionalcode, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByRegionalcode(String regionalcode, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByRegionalcode", startResult, maxRows, regionalcode);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByRegionalcodeContaining(String regionalcode) throws DataAccessException {

		return findAddressByRegionalcodeContaining(regionalcode, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByRegionalcodeContaining(String regionalcode, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByRegionalcodeContaining", startResult, maxRows, regionalcode);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByPostcode(String postcode) throws DataAccessException {

		return findAddressByPostcode(postcode, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByPostcode(String postcode, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByPostcode", startResult, maxRows, postcode);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByPostcodeContaining(String postcode) throws DataAccessException {

		return findAddressByPostcodeContaining(postcode, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByPostcodeContaining(String postcode, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByPostcodeContaining", startResult, maxRows, postcode);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByLevel(Integer level) throws DataAccessException {

		return findAddressByLevel(level, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByLevel(Integer level, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAddressByLevel", startResult, maxRows, level);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByDescription(String description) throws DataAccessException {

		return findAddressByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Set<Address> findAddressByDescriptionContaining(String description) throws DataAccessException {

		return findAddressByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAddressByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Transactional
	public Address findAddressByPrimaryKey(Integer id) throws DataAccessException {
		return findAddressByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Address findAddressByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAddressByPrimaryKey", startResult, maxRows, id);
			return (Address) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Address> findAddresssByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAddresssByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Address> findAllAddresss() throws DataAccessException {

		return findAllAddresss(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAllAddresss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAddresss", startResult, maxRows);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Address> findAddresssByParent(Integer addressId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllAddresssByParent", start, max, addressId);
		return new LinkedHashSet<Address>(query.getResultList());
	}

	@Override
	public Set<Integer> findAddressIdsByParent(int rootAddressId) throws DataAccessException {
		Query query = createNamedQuery("findAddressIdsByParent", -1, -1, rootAddressId);
		return new LinkedHashSet<Integer>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Address> findAddressByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Address>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Address> findAddresssByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Address.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Address> addresss = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return addresss;
	}

	@Transactional
	public Address findAddressByPhysicalPrimaryKey(String name, Integer parentId) throws DataAccessException {
		return findAddressByPhysicalPrimaryKey(name, parentId, -1, -1);
	}

	@Transactional
	public Address findAddressByPhysicalPrimaryKey(String name, Integer parentId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAddressByPhyscialPrimaryKey", startResult, maxRows, name, parentId);
			return (Address) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Address entity) {
		return true;
	}
}
